/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poultry;

import fallOutObjects.eggObject;
import fallOutObjects.fallOutObjects;
import fallOutObjects.shitObject;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;


/**
 *
 * @author Windows7
 */
public class poultry implements poultryInteface {

    
    protected String poultryImages;
    
    protected int x;
    protected int y;
    protected int h;
    protected int w;
    protected boolean visible;
    protected int direction;
    protected int countTime;
    protected BufferedImage image;
    private ArrayList<fallOutObjects> objectsFallOut;
    
    public poultry(int x, int y) throws IOException {
            
        //image = ImageIO.read(new File(poultryImages));
        this.objectsFallOut = new ArrayList<fallOutObjects>();
        visible = true;
        this.x = x;
        this.y = y;
        this.countTime = 0;
        Random generator = new Random(); 
        int ramdom = generator.nextInt(700) + 1;
       
        if(ramdom % 2 == 1)
        {
            direction = 1;
        }
        else
        {
            direction = -1;
        }
        
    }
    
    @Override
    public synchronized void draws(Graphics2D grap)
    {
        /*AffineTransform t = new AffineTransform();
        t.translate(this.x, this.y);
        t.scale(1, 1);*/
        grap.drawImage(this.image,this.x,this.y, null);
        int n = this.getObjectsFallOut().size();
        for(int i = 0 ; i< n ; i++)
        {

           this.getObjectsFallOut().get(i).draws(grap);

            
        }
    }
    
    public void deleteAllCraps()
    {
        int n = this.getObjectsFallOut().size();
        for(int i = 0 ; i< n ; i++)
        {
            boolean result = getObjectsFallOut().get(i).isVisible();
            if(result == false)
            {
                this.getObjectsFallOut().remove(i);
            }
        }
    }
    
    public boolean move(int value) throws IOException 
    {
        boolean result;
        if (x < 0 || x > 730) 
        {
            direction = direction * (-1);
        }
        x = x + (direction * 1);
        dropObject(value);
        result = checkAllFallOutObs();
        return result;
    }
    
    public void dropObject(int value) throws IOException
    {
        this.countTime ++;
        if(this.countTime == 950)
        {
            this.countTime = 0;
            Random generator = new Random(); 
            int ramdom = generator.nextInt(700) + 1;
       

            
            if(ramdom % 2 == 1)
            {
                this.getObjectsFallOut().add(new eggObject(this.x, this.y + 20,value));
            }
            else
            {
                this.getObjectsFallOut().add(new shitObject(this.x, this.y + 20,value));
            }
        }
    }
    
    public boolean checkAllFallOutObs()
    {
        boolean result = false;
        int n = this.getObjectsFallOut().size();
        for(int i = 0;  i < n ; i++)
        {
            result = this.getObjectsFallOut().get(i).move();
        }
        deleteAllCraps();
        return result;
    }
    
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }

    public BufferedImage getImage() {
        return image;
    }

    public Rectangle getBounds() {
        return new Rectangle(x - (this.w/4), y - (this.h/4), this.w/2, this.h/2);
    }

    /**
     * @return the objectsFallOut
     */
    public ArrayList<fallOutObjects> getObjectsFallOut() {
        return objectsFallOut;
    }

    /**
     * @param objectsFallOut the objectsFallOut to set
     */
    public void setObjectsFallOut(ArrayList<fallOutObjects> objectsFallOut) {
        this.objectsFallOut = objectsFallOut;
    }
    
    
    
}
